Skip to content

[Go] Add strictResponseDecoding option to Go-server generator. Fixes issue #21446#21456

Closed
Chimdi511 wants to merge 2 commits intoOpenAPITools:masterfrom
Chimdi511:feature/strict-response-decoding
Closed

[Go] Add strictResponseDecoding option to Go-server generator. Fixes issue #21446#21456
Chimdi511 wants to merge 2 commits intoOpenAPITools:masterfrom
Chimdi511:feature/strict-response-decoding

Conversation

@Chimdi511
Copy link
Copy Markdown

Summary

This PR introduces a new generator property, strictResponseDecoding, to the go-server generator. When enabled (the new default), every JSON decoder in generated handlers will call DisallowUnknownFields(), causing the server to reject any incoming payloads that contain fields not defined in the spec. Users can disable strict mode and allow extra fields by passing --additional-properties strictResponseDecoding=false. Fixes issue #21446.


Changes

  1. Codegen configuration

    • File: GoServerCodegen.java

    • What’s changed:

      • Added a STRICT_RESPONSE_DECODING CLI option (default: true).
      • Exposed strictResponseDecoding boolean in additionalProperties for use in templates.
  2. Template guard

    • File: server.mustache

    • What’s changed:

      • Wrapped the call to decoder.DisallowUnknownFields() in a {{#strictResponseDecoding}}…{{/strictResponseDecoding}} block.
  3. Documentation

    • File: Go-server README (modules/openapi-generator/README.md)

    • What’s changed:

      • Documented the new --additional-properties=strictResponseDecoding={true|false} flag, its default behavior, and usage example.

Impact

  • Backward compatibility:

    • Default behavior is now “strict” (reject unknown fields). Users relying on permissive parsing must explicitly opt out.
  • Client generation:

    • No impact; this change applies only to the go-server generator.

Usage Example

# Strict mode (default):
openapi-generator generate \
  -i api.yaml \
  -g go-server

# Permissive mode:
openapi-generator generate \
  -i api.yaml \
  -g go-server \
  --additional-properties strictResponseDecoding=false

Checklist

  • Added CLI option in GoServerCodegen.java
  • Updated server.mustache template
  • Updated Go-server README with documentation

@antihax @grokify @kemokemo @jirikuncar @ph4r5h4d @lwj5

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@Chimdi511 Chimdi511 changed the title Add strictResponseDecoding option to Go-server generator. Fixes issue #21446 [Go] Add strictResponseDecoding option to Go-server generator. Fixes issue #21446 Jun 24, 2025
@wing328
Copy link
Copy Markdown
Member

wing328 commented Jul 2, 2025

please merge the latest master to your branch and regenerate the samples again

let me know if you need any help

d.DisallowUnknownFields()
{{/isAdditionalPropertiesTrue}}
{{/strictResponseDecoding}}
if err := d.Decode(&{{paramName}}Param); err != nil {{^required}}&& !errors.Is(err, io.EOF) {{/required}}{
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the pr

before introducing another option, did you try setting disallowAdditionalPropertiesIfNotPresent to false (or true which should be the default)?

@wing328 wing328 closed this Aug 1, 2025
@wing328
Copy link
Copy Markdown
Member

wing328 commented Aug 1, 2025

closing as there's no update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants